package procilege.test.pal;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;

import java.util.Iterator;

import org.junit.Test;

import procilege.impl.ComputationalEntityMgr;
import procilege.lang.PrivilegeForm;
import procilege.lang.PrivilegeFormTerm;

import com.google.inject.Inject;

public class ComputationalEntityMgrTest2 extends AbstractPalTest {

	@Inject
	protected ComputationalEntityMgr cem;

	@Test
	public void test1() {

		mvm.give(nm("te"));
		mvm.stepin();
		try {

			def(nn("p1"), pvm(nn("a")));
			dumpHistory(0, 30);
			assertEquals(pvm(nn("a")), cem.retrievePrivlegeExpr(nn("p1")));

			PrivilegeForm pf1;

			pf1 = cem.retrievePrivlegeForm(nn("p1"));
			assertNotNull(pf1);
			int count = 0;
			Iterator<PrivilegeFormTerm> pftItr = pfa.normalTerms(pf1)
					.iterator();
			while (pftItr.hasNext()) {
				PrivilegeFormTerm t = pftItr.next();
				if (nn("a").equals(t.getFunctionName())) {
					count++;
				}
			}
			assertEquals(1, count);

			def(nn("p1"), pvc(nn("a"), nn("b")));
			dumpHistory(0, 30);
			assertEquals(pvc(nn("a"), nn("b")), cem
					.retrievePrivlegeExpr(nn("p1")));

			pf1 = cem.retrievePrivlegeForm(nn("p1"));
			assertNotNull(pf1);
			count = 0;
			pftItr = pfa.normalTerms(pf1).iterator();
			while (pftItr.hasNext()) {
				PrivilegeFormTerm t = pftItr.next();
				if (nn("a").equals(t.getFunctionName())) {
					count++;
				} else if (nn("b").equals(t.getFunctionName())) {
					count++;
				}

			}
			assertEquals(2, count);

		} finally {
			mvm.stepout();
		}
	}
}
